package yy;

import java.util.Stack;

public class RemoveDuplicates {
    public static void main(String[] args){
        String s = "abbaca";
        String resultString = removeDuplicates(s);
        System.out.println(resultString);

    }
    public  static String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++){
            if(!stack.isEmpty()){
                char temp = stack.peek();
                if(temp == s.charAt(i)){
                    stack.pop();
                    continue;
                }else {
                    stack.push(s.charAt(i));
                }
            }else {
                stack.push(s.charAt(i));
            }
        }
        char[] str = new char[stack.size()];
        for(int i = stack.size()-1;i>=0;i--){
            str[i] = stack.pop();
        }
        return new String(str);

    }
}
